<?php
/**
 * Created by CuiLiwu.
 * User: CuiLiwu
 * Date: 2018/4/19
 * Time: 16:08
 */

namespace App\Http\Controllers\Web\Seller;
use App\ConstDir\ErrorConst;
use Illuminate\Http\Request;
use App\Repository\Repositories\Interfaces\ShopBannersRepository;
use App\Http\Controllers\Web\BaseController;

class ShopBannersController extends BaseController
{
    /**
     * @var ShopBannersRepository
     */
    protected $banners_repo;
    protected $request;

    protected $shop_id;

    public function __construct(ShopBannersRepository $banners_repo,Request $request)
    {
        parent::__construct();

        $this->banners_repo = $banners_repo;
        $this->request = $request;

        $this->shop_id = array_get($this->user, 'shop_id');

        // 用户只能操作自身所属数据
        $this->banners_repo->scopeQuery(function ($query) {
            return $query->where(['shop_id'=>$this->shop_id]);
        });
    }

    public function index()
    {
        $collection = $this->banners_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE, $collection, true);
    }

    public function show($id)
    {
        
    }

    /**
     * 创建
     * @param Request $request
     * @return \Illuminate\Support\Collection
     */
    public function store()
    {
        try {
            $data = $this->form();
            $data['shop_id'] = $this->shop_id;

            $ret = $this->banners_repo->create($data);
        }catch (\Exception $e){
            $ret = false;
        }
        return $this->noDataReturn($ret ? true : false);
    }

    /**
     * 更新
     * @param Request $request
     */
    public function update($id)
    {
        try {
            $data = $this->form();

            $data['id'] = $id;
            $data['shop_id'] = $this->shop_id;
            $ret = $this->banners_repo->update($data, $id);
        }catch (\Exception $e){
            $ret = false;
        }
        return $this->noDataReturn($ret ? true : false);
    }


    public function delete($id)
    {
        $data['id'] = $id;
        $data['shop_id'] = $this->shop_id;
        $ret = $this->banners_repo->delete($id);
        return $this->noDataReturn($ret ? true : false);
    }

    public function form(){
        $this->validate($this->request, [
            'banner_name' => 'required',
            'banner_url'  => 'sometimes|regex:/ejing365.com/',
            'banner_image'=> 'required',
            'screen_type' => 'required|in:1,2,3',
        ],[
            'banner_name' => 'banner标题必填',
            'banner_url'  => '仅支持内部链接，即以ejing365.com为主域名的链接',
            'banner_image'=> '必须选择展示的图片',
            'screen_type' => 'screen_type必须为数字|type必须为1或2或3',
        ]);
        return $this->request->all(['banner_name', 'banner_image', 'banner_url', 'screen_type']);
    }
    public function log($data,$filename){
        if(empty($filename)){
            $log_filename='C:\Users\admin\Desktop\ecclub.php';
        }
        else{
            $log_filename='C:\Users\admin\Desktop\\'.$filename.'.php';
        }
        // 如果文件夹不存在则创建文件夹
        is_dir(dirname($log_filename)) || mkdir(dirname($log_filename),0777,true);
        // 日志大小
        $max_size=10485760;
        // 判断文件大小做自动删除动作
        if(file_exists($log_filename) && abs(filesize($log_filename))>$max_size){
            // 删除文件
            unlink($log_filename);
        }else{
            // 写日志(第三个参数是系统函数，可以连续写文件不覆盖)
            file_put_contents($log_filename, date("Y-m-d H:i:s",time()+8*60*60)." ".$data."\n", FILE_APPEND);
        }
    }
}